import Mock from 'mockjs'

Mock.mock(/\/mock\/order.*/, 'post', (options: any) => {
  const body = JSON.parse(options.body)
  // console.log(body)
    let num =20-(body.page-1)*5
    num = num >5?5:num
    const data = Mock.mock({
      [`list|${num}`]: [
        {
            check:false,
            order: '@guid',
            'deliveryp|1': ['男士餐','女士餐','工作餐'],
            'money|1': [600,122,554,66,12,55,13,85],
            address: ' @ctitle(6,20)',
            'paystatus|1': ['已付款','未付款'],
            'orderstatus|1': ['进行中','已完成','失败'],
            'receiver|1':['张三','李四','李师傅','张师傅'],
            time: '@DATETIME("yyyy-MM-dd")',
            'phone|1':Mock.mock(/1\d{10}/),
            
          }],
          total:20,
          pageSize:5,
    })
    if(body.region) {
      console.log(data.list)
      let deliveryplist =['男士餐', '女士餐', '工作餐']
      deliveryplist=  deliveryplist.filter(item=>{
        return item.includes(body.region)
      })
      if(deliveryplist[0]){
        // @ts-ignore
        data.list = data.list.map((item) => {
         const num = Math.floor(Math.random()*deliveryplist.length)
          item.deliveryp = deliveryplist[num]
          return item
        })
        
        data.total *= deliveryplist.length/3
      }
    
  }
  if(body.name){
    if (!isNaN(body.name)) {
      // @ts-ignore
            data.list = data.list.map((item) => {
              item.phone = body.name
              return item
            })
            data.total *=0.5
          }else{ 
            let receiverlist =['张三','李四','李师傅','张师傅']
            receiverlist=  receiverlist.filter(item=>{

              return item.includes(body.name)
            })
            console.log(receiverlist)
            if(receiverlist[0]){
              // @ts-ignore
              data.list = data.list.map((item) => {
               const num = Math.floor(Math.random()*receiverlist.length)
                item.receiver = receiverlist[num]
                return item
              })
              data.total *= receiverlist.length/3
             
            }
            else{
              data.list=[]
            }
  
          }
          
  }
  if(body.date1 && body.date2){
    let time1=body.date1
    let time2=body.date2
    let time3=Math.floor(time1+Math.random()*(time2-time1+1))
    let time4=new Date(time3)
      // @ts-ignore
      data.list = data.list.map((item) => {
        item.time = time4
        return item
      })
      data.total *= 0.5
  }

    return {
      data,
      code:200,
      message:'请求订单列表成功',
    }
  })